package org.glow.changgou.order.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.glow.changgou.order.dao.PreferentialMapper;
import org.glow.changgou.order.pojo.OrderItem;
import org.glow.changgou.order.pojo.OrderLog;
import org.glow.changgou.order.pojo.Preferential;
import org.glow.changgou.order.service.PreferentialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/****
 * @Author:glow
 * @Description:Preferential业务层接口实现类
 *****/
@Service
public class PreferentialServiceImpl extends ServiceImpl<PreferentialMapper,Preferential>implements PreferentialService {

    @Resource
    private PreferentialMapper preferentialMapper;


    /**
     * Preferential条件+分页查询
     * @param preferential 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public Page<Preferential> findPage(Preferential preferential, int page, int size){
        Page<Preferential> pageInfo = new Page<>(page,size);
        LambdaQueryWrapper<Preferential> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = getCondition(queryWrapper,preferential);
        return preferentialMapper.selectPage(pageInfo,queryWrapper);
    }

    /**
     * Preferential分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Preferential> findPage(int page, int size){
        Page<Preferential> pageInfo = new Page<>(page,size);

        return preferentialMapper.selectPage(pageInfo,null);
    }

    /**
     * Preferential条件查询
     * @param preferential
     * @return
     */
    @Override
    public List<Preferential> findList(Preferential preferential){

        LambdaQueryWrapper<Preferential> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = getCondition(queryWrapper,preferential);
        return preferentialMapper.selectList(queryWrapper);
    }


    /**
     *
     * @param queryWrapper
     * @param preferential
     * @return
     */
    public LambdaQueryWrapper<Preferential> getCondition(LambdaQueryWrapper<Preferential> queryWrapper, Preferential preferential){

        if(preferential!=null){
            // ID
            if(!StringUtils.isEmpty(preferential.getId())){
//                    criteria.andEqualTo("id",preferential.getId());
                    queryWrapper.eq(Preferential::getId,preferential.getId());
            }
            // 消费金额
            if(!StringUtils.isEmpty(preferential.getBuyMoney())){
//                    criteria.andEqualTo("buyMoney",preferential.getBuyMoney());
                    queryWrapper.eq(Preferential::getBuyMoney,preferential.getBuyMoney());
            }
            // 优惠金额
            if(!StringUtils.isEmpty(preferential.getPreMoney())){
//                    criteria.andEqualTo("preMoney",preferential.getPreMoney());
                    queryWrapper.eq(Preferential::getPreMoney,preferential.getPreMoney());
            }
            // 品类ID
            if(!StringUtils.isEmpty(preferential.getCategoryId())){
//                    criteria.andEqualTo("categoryId",preferential.getCategoryId());
                    queryWrapper.eq(Preferential::getCategoryId,preferential.getCategoryId());
            }
            // 活动开始日期
            if(!StringUtils.isEmpty(preferential.getStartTime())){
//                    criteria.andEqualTo("startTime",preferential.getStartTime());
                    queryWrapper.eq(Preferential::getStartTime,preferential.getStartTime());
            }
            // 活动截至日期
            if(!StringUtils.isEmpty(preferential.getEndTime())){
//                    criteria.andEqualTo("endTime",preferential.getEndTime());
                    queryWrapper.eq(Preferential::getEndTime,preferential.getEndTime());
            }
            // 状态
            if(!StringUtils.isEmpty(preferential.getState())){
//                    criteria.andEqualTo("state",preferential.getState());
                    queryWrapper.eq(Preferential::getState,preferential.getState());
            }
            // 类型1不翻倍 2翻倍
            if(!StringUtils.isEmpty(preferential.getType())){
//                    criteria.andEqualTo("type",preferential.getType());
                    queryWrapper.eq(Preferential::getType,preferential.getType());
            }
        }
        return queryWrapper;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        preferentialMapper.deleteById(id);
    }

    /**
     * 修改Preferential
     * @param preferential
     */
    @Override
    public void update(Preferential preferential){
        preferentialMapper.updateById(preferential);
    }

    /**
     * 增加Preferential
     * @param preferential
     */
    @Override
    public void add(Preferential preferential){
        preferentialMapper.insert(preferential);
    }

    /**
     * 根据ID查询Preferential
     * @param id
     * @return
     */
    @Override
    public Preferential findById(Integer id){
        return  preferentialMapper.selectById(id);
    }

    /**
     * 查询Preferential全部数据
     * @return
     */
    @Override
    public List<Preferential> findAll() {
        return preferentialMapper.selectList(null);
    }
}
